// pages/find/card/other-card.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        isHasCard: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id,
        })

    },
    loadCard: function () {
        app.http.get("member-visiting-card/card-info", {
            data: {
                id: this.data.id
            }
        }).then(res => {
            try {
                this.setData({
                    is_keep: res.data.is_keep,
                    is_like: res.data.is_like,
                    info: res.data.info.memberVisitingCard,
                    nickName: res.data.info.memberVisitingCard.name,
                    job: res.data.info.memberVisitingCard.job,
                    phone: res.data.info.memberVisitingCard.phone,
                    company: res.data.info.memberVisitingCard.company,
                    email: res.data.info.memberVisitingCard.email,
                    detail: res.data.info.memberVisitingCard.detail,
                    address: res.data.info.memberVisitingCard.address,
                    images: res.data.info.memberVisitingCard.image,
                    like_num: res.data.info.memberVisitingCard.like_num,
                    keep_num: res.data.info.memberVisitingCard.keep_num,
                    visiting_num: parseInt(res.data.info.memberVisitingCard.visiting_num) + 1,
                    isHasCard: res.data.is_has_card
                });
            } catch (err) {
                throw err
            }
        }).catch(err => {
            console.log(err)
            app.wxToast.error(err.message);
        })
    },
    onShow() {
        this.loadCard()
    },


    backHome: function () {
        wx.switchTab({
            url: '/pages/home/home'
        })
    },
    /**
     * 收藏
     */
    addKeep: function (event) {
        app.http.get("member-visiting-card/add-keep", {
            data: {
                id: event.currentTarget.dataset.id
            }
        }).then(res => {
            if (res.data.status == 1) {
                this.setData({
                    is_keep: true,
                    keep_num: parseInt(this.data.keep_num) + 1
                });
            } else {
                this.setData({
                    is_keep: false,
                    keep_num: parseInt(this.data.keep_num) - 1
                });
            }
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },
    /**
     * 点赞
     */
    addLike: function (event) {
        app.http.get("member-visiting-card/add-like", {
            data: {
                id: event.currentTarget.dataset.id
            }
        }).then(res => {
            if (res.data.status == 1) {
                this.setData({
                    is_like: true,
                    like_num: parseInt(this.data.like_num) + 1
                });
            } else {
                this.setData({
                    is_like: false,
                    like_num: parseInt(this.data.like_num) - 1
                });
            }
        }).catch(err => {
            app.wxToast.error(err.message);
        })
    },
    /**
     * 添加到通讯录
     */
    addPhoneList: function () {
        wx.addPhoneContact({
            firstName: this.data.info.name,
            mobilePhoneNumber: this.data.info.phone,
            success: res => {
                console.log(1);
            }, fail: res => {
                app.wxToast.error(err.errMsg);
            }
        })
    },
    onShareAppMessage: function () {
        return {
            title: '为您推荐' + this.data.nickName + '的名片',
            path: '/pages/find/card/other-card?id=' + this.data.id,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
            }
        }
    },
    addPhoneList: function () {
        wx.addPhoneContact({
            firstName: this.data.info.name,
            mobilePhoneNumber: this.data.info.phone,
            success: res => {
            }, fail: res => {
            }
        })
    },
})